package com.kaka.starter.token.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author kaka
 * @Date 2022/6/10
 */
public class HeaderCommon {

    public static final String ROLE_ID = "roleId";
    public static final String GROUP_ID = "groupId";
    public static final String USER_ID = "userId";
    public static final String TENANT_ID = "tenantId";
    public static final String USER_NAME = "userName";
    public static final String QUERY_GROUPS = "queryGroupIds";
    public static final String DEPT_ID = "deptId";
    public static final String LEVEL = "level";

    public static Integer getUserId(JWTUtils.JWTResult headerDTO) {
        return (Integer) headerDTO.get(USER_ID).get();
    }

    public static List<Integer> getQueryGroups(JWTUtils.JWTResult headerDTO) {
        return (List<Integer>) headerDTO.get(QUERY_GROUPS).get();
    }

    public static String getUserName(JWTUtils.JWTResult headerDTO) {
        return (String) headerDTO.get(USER_NAME).get();
    }

    public static Integer getTenantId(JWTUtils.JWTResult headerDTO) {
        return Integer.valueOf(headerDTO.get(TENANT_ID).get().toString());
    }

    public static Integer getUserGroupIds(JWTUtils.JWTResult headerDTO) {
        return (Integer) headerDTO.get(GROUP_ID).get();
    }

    public static Integer getUserDept(JWTUtils.JWTResult headerDTO) {
        if (headerDTO.getOrigin().getBody().get(DEPT_ID)==null){
            return null;
        }
        return (Integer) headerDTO.get(DEPT_ID).get();
    }

    public static Integer getRoleIds(JWTUtils.JWTResult headerDTO) {
        return (Integer) headerDTO.get(ROLE_ID).get();
    }

    public static String getUserLevel(JWTUtils.JWTResult headerDTO) {
        if (headerDTO.getOrigin().getBody().get(LEVEL)==null){
            return null;
        }
        return String.valueOf(headerDTO.get(LEVEL).get());
    }


    //public static void forEach(JSONArray headerDTO, List<Long> roleList) {
    //    for (Object tmp : headerDTO) {
    //        if (tmp instanceof Long) {
    //            roleList.add((Long) tmp);
    //        } else {
    //            roleList.add(Long.valueOf(String.valueOf(tmp)));
    //        }
    //    }
    //}
    //
    //public static List<Long> getRoleList(JSONObject headerDTO) {
    //    List<Long> roleList = new ArrayList<>();
    //    //如果没有空间 ->推算是授权模式
    //    if (StringUtils.isEmpty(String.valueOf(headerDTO.getJSONArray(USER_ID)))) {
    //        forEach(headerDTO.getJSONArray(USER_ID), roleList);
    //        forEach(headerDTO.getJSONArray(ROLE_ID), roleList);
    //        forEach(headerDTO.getJSONArray(GROUP_ID), roleList);
    //    } else {
    //        //forEach(headerDTO.getJSONArray(USER_ID), roleList);
    //        //forEach(headerDTO.getJSONArray(ROLE_ID), roleList);
    //        //forEach(headerDTO.getJSONArray(GROUP_ID), roleList);
    //        forEach(headerDTO.getJSONArray(CURRENCT_NS), roleList);
    //    }
    //    return roleList;
    //}
}
